package com.oz.ems.action;


import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.http.Http;
import org.nutz.http.Response;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Streams;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;

import com.oz.ems.Utils;

@IocBean
public class UtilsAction {

	Log log = Logs.getLog(UtilsAction.class);

	@At("/upload/*")
	@Ok("raw")
	@Fail(">>:/favicon.ico")
	public void takeFiles(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String path = Utils.getFilesPath() + File.separator + req.getRequestURI().replaceAll("(.*)upload/", "").replaceAll("([/|(\\\\)]+)",Matcher.quoteReplacement(File.separator));
//		log.debug("..................................获取需要取得的文件路径.................."+ path);
//		log.debug("..................................关键文件路径文件路径.................." + req.getServletContext().getRealPath("/"));

		File file = new File(path);
//		String foundImage = "";
//		if (!file.exists()) 
//			file = new File(req.getServletContext().getRealPath("/") + File.separator + "favicon.ico");
		resp.setHeader("Content-Length", "" + file.length());
		Streams.writeAndClose(resp.getOutputStream(), Streams.fileIn(file));

	}
	
	@At("/main/readurl")
	@Ok("raw")
	public Object readURL(String url,HttpServletRequest req, HttpServletResponse resp) throws IOException {
		log.debug("..................................待读取的路径.................."+ url);
		Response response = Http.get(url);
//		log.debug("..................................读取的路径结果..................\n"+ response.getContent());
		return response.getContent();

	}

}
